﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using Newtonsoft.Json;
using ZLCMS.Business.Util;
using ZLCMS.Commands.Data;

namespace ZLCMS.Web.SystemManage.Basics.Shop.MemberCard
{
    public partial class MemberCardRuleDetail : System.Web.UI.Page
    {
        protected WxMemberCardRule cardRule { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //设置对象
                int uid = 0;

                ParamKit.Bind("MemberCardKind", ddl_member_card_kind, null, null, null, "", "normalcard");

                if (int.TryParse(Request["uid"], out uid))
                {
                    using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        cardRule = session.Get<WxMemberCardRule>(uid);
                        if (cardRule != null)
                        {
                            //保存uid
                            ViewState["uid"] = uid;
                            //初始化页面内容
                            txt_Content.Value = cardRule.Content;
                            ckb_is_defauld.Checked = cardRule.IsDefault == "1";

                            ddl_member_card_kind.SelectedValue = cardRule.Kind;
                            MemberCardItem item = JsonConvert.DeserializeObject<MemberCardItem>(cardRule.ItemData);
                            if (item != null)
                            {
                                txt_balance.Text = item.Balance.ToString();
                                txt_start.Text = item.Start.ToString("yyyy-MM-dd");
                                txt_end.Text = item.End.ToString("yyyy-MM-dd");
                                txt_times.Text = item.Times.ToString();

                             
                            }
                        }
                    }
                }
            }
            else
            {
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    if (ViewState["uid"]==null||ViewState["uid"]=="")
                    {
                        cardRule = new WxMemberCardRule();
                        cardRule.ConfigId = 8;
                    }
                    else
                    {
                        cardRule = session.Get<WxMemberCardRule>(ViewState["uid"]);
                    }
                    cardRule.Description = Request["Description"];
                    cardRule.Content = Request["Content"];
                    cardRule.Kind = ddl_member_card_kind.SelectedValue;
                    cardRule.Memo = Request["Memo"];
                    cardRule.HeadChar = Request["HeadChar"];
                    cardRule.Title = Request["Title"];
                    cardRule.IsDefault = ckb_is_defauld.Checked == true ? "1" : "0";

                    cardRule.ItemData = JsonConvert.SerializeObject(new MemberCardItem()
                    {
                        Balance = int.Parse(txt_balance.Text),
                        Start = DateTime.Parse(txt_start.Text),
                        End = DateTime.Parse(txt_end.Text),
                        Times = int.Parse(txt_times.Text)
                    });

                    session.SaveOrUpdate(cardRule);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Shop/MemberCard/MemberCardRuleDetail.aspx?tabid=MemberCardRuleDetail";

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + cardRule.Id;
                }

                _framePage.Alert(rc);
            }
        }
    }
}